package com.example.blog.web;

import com.example.blog.annotation.LoginUser;
import com.example.blog.annotation.RequiresPermission;
import com.example.blog.dao.CommentsRepository;
import com.example.blog.dao.PostsInfoRepository;
import com.example.blog.dto.CommentsDto;
import com.example.blog.entity.Comments;
import com.example.blog.entity.PostsInfo;
import com.example.blog.exception.NewWifiLoginException;
import com.example.blog.util.Result;
import com.example.blog.util.StringUtils;
import com.example.blog.util.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.blog.constant.ApiUrlConstants.*;

/**
 * @ClassName: comments
 * @Description: 帖子评论

 * @Date 2023/9/9
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/v1")
@Api(value = "帖子评论",description = "帖子评论",tags = "帖子评论")
@CrossOrigin
public class CommentsController {
    @Resource
    CommentsRepository commentsRepository;
    @Resource
    PostsInfoRepository postsInfoRepository;

    @PostMapping("/coment/{postId}")
    @RequiresPermission(roles = {ADMIN,USER})
    @ApiOperation(value = "评论")
    public Result addcoment(@LoginUser String userId ,
                            @PathVariable @ApiParam("帖子id") String postId ,
                            @RequestBody CommentsDto commentsDto){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        Comments comments = new Comments();
        BeanUtils.copyProperties(commentsDto,comments);
        comments.setUpdTime(new Date());
        comments.setCTime(new Date());
        comments.setUserId(userId);
        comments.setStatus(true);
        comments.setPostInfoId(postId);
        commentsRepository.save(comments);
        Optional<PostsInfo> byId = postsInfoRepository.findById(postId);
        byId.get().setCommentCount(byId.get().getCommentCount()+1);
        postsInfoRepository.save(byId.get());
        return Result.ok();
    }
    @GetMapping("/coment/{postId}")
    @RequiresPermission(roles = {ADMIN,USER,GUEST})
    @ApiOperation(value = "帖子-评论")
    public Result getcoment(@LoginUser String userId,
                            @PathVariable @ApiParam("帖子id") String postId,
                            @RequestParam Integer pageSize,
                            @RequestParam Integer pageNum){
        pageNum = pageNum == null || pageNum < 0 ? 0:pageNum-1;
        Pageable pageable = PageRequest.of(pageNum,pageSize); //页码：前端从1开始，jpa从0开始，做个转换
        Page<Map> page = commentsRepository.findByPostInfoId(postId,pageable);
        return Result.ok().setData(page);
    }
    @DeleteMapping("/{postId}/coment/{commentId}")
    @ApiOperation(value = "删除-评论")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result delcoment(@LoginUser String userId,
                            @PathVariable @ApiParam("帖子id") String postId,
                            @PathVariable @ApiParam("评论") String commentId){
        commentsRepository.deleteById(commentId);
        return Result.ok();
    }
}
